package za.ac.cput.arrowgame.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.table.AbstractTableModel;
import za.ac.cput.arrowgame.app.factories.AppFactory;

public class Grid extends AbstractTableModel {

    private static final long serialVersionUID = 1L;
    private Arrow arrow;
    private Integer rowHeight;
    private Integer columnWidth;
    private Icon[][] tableData;
    private int rowCount;
    private int columnCount;

    public Grid() {
    }

    public Grid(Integer columnWidth, Integer rowHeight, Integer gridSize) {
        this.columnWidth = columnWidth;
        this.rowHeight = rowHeight;
        this.rowCount = gridSize;
        this.columnCount = gridSize;
        this.tableData = new ImageIcon[gridSize][gridSize];
    }

    public void setGridSize(Integer gridSize) {
        this.rowCount = gridSize;
        this.columnCount = gridSize;
        this.tableData = new ImageIcon[gridSize][gridSize];
        this.arrow = AppFactory.getInstance().createArrow(Direction.NORTH, gridSize - 1);
        populateGrid();
        this.fireTableStructureChanged();
        this.fireTableDataChanged();
    }

    public void setRowCount(Integer rowCount) {
        this.rowCount = rowCount;
    }

    public int getRowCount() {
        return rowCount;
    }

    public void setColumnCount(Integer columnCount) {
        this.columnCount = columnCount;
    }

    public int getColumnCount() {
        return columnCount;
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        return tableData[rowIndex][columnIndex];
    }

    @Override
    public void setValueAt(Object imageIcon, int rowIndex, int columnIndex) {
        tableData[rowIndex][columnIndex] = (ImageIcon) imageIcon;
    }

    @Override
    public Class getColumnClass(int column) {
        return getValueAt(0, column).getClass();
    }

    public void setArrow(Arrow arrow) {
        this.tableData[this.arrow.getGridLocation().getyCoordinate()][this.arrow.getGridLocation().getxCoordinate()] = new ImageIcon("src\\main\\resources\\valid.jpg");
        this.tableData[arrow.getGridLocation().getyCoordinate()][arrow.getGridLocation().getxCoordinate()] = new ImageIcon("src\\main\\resources\\" + arrow.getDirection().name() + ".jpg");
        this.arrow = arrow;
        this.fireTableDataChanged();
        this.fireTableStructureChanged();
    }

    public Arrow getArrow() {
        return arrow;
    }

    public Integer getRowHeight() {
        return rowHeight;
    }

    public void setRowHeight(Integer rowHeight) {
        this.rowHeight = rowHeight;
    }

    public Integer getColumnWidth() {
        return columnWidth;
    }

    public void setColumnWidth(Integer columnWidth) {
        this.columnWidth = columnWidth;
    }

    public Icon[][] getTableData() {
        return tableData;
    }

    public void setTableData(ImageIcon[][] tableData) {
        this.tableData = tableData;
    }

    private void populateGrid() {
        List<Icon> icons = new ArrayList<Icon>();
        icons.add(new ImageIcon("src\\main\\resources\\valid.jpg"));
        icons.add(new ImageIcon("src\\main\\resources\\valid.jpg"));
        icons.add(new ImageIcon("src\\main\\resources\\invalid.jpg"));
        icons.add(new ImageIcon("src\\main\\resources\\valid.jpg"));
        Random random = new Random();

        for (int i = 0; i < rowCount; i++) {
            for (int j = 0; j < columnCount; j++) {
                tableData[i][j] = icons.get(random.nextInt(4));
            }
        }
        tableData[0][0] = new ImageIcon("src\\main\\resources\\end.jpg");
        tableData[rowCount - 1][columnCount - 1] = new ImageIcon("src\\main\\resources\\north.jpg");

    }
}
